//
//  WWTabbar.swift
//  -00 旺旺微博
//
//  Created by 伍良旺 on 16/4/16.
//  Copyright © 2016年 伍良旺. All rights reserved.
//

import UIKit
//声明一个协议
protocol WWTabbarDelegate: NSObjectProtocol{
    //协议方法
    func clickCenterBtn()

}

class WWTabbar: UITabBar {

    //声明一个代理
    weak var wwDelegate: WWTabbarDelegate?
    

    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }
    
    //设置控件 
    func setupUI(){
        addSubview(btn)
    }
    
    // 懒加载控件
    lazy var btn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.sizeToFit()
        
        return btn
    
    }()
    
    //中间按钮点击事件
    func clickBtn(){
        wwDelegate?.clickCenterBtn()
    }
    
    //对子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let subWidth = UIScreen.mainScreen().bounds.width / 5
        var index:CGFloat = 0
        
        
        //设置tabBarItem的frame 
        for subV in self.subviews{
            
            if subV.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                subV.frame.origin.x = index * subWidth
             
                subV.frame.size.width = subWidth
            
                index++
            
                if index == 2 {
                    index++
                }
            }
        }
        btn.center.x = self.frame.size.width * 0.5
        btn.center.y = self.frame.size.height * 0.5
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
